#include "LedBoardModule.h"
#include "SimpleCompletion.h"

/**
* bit0: 是否正常运行
* bit1: 是否在发送数据
*/
Led_Status_Defl led_status = {0, 0, 1, 0};
size_t led_busy_times;
rt_sem_t led_work_status_sem;
uint32_t led_run_times = 0;

// Led运行函数
void Thread_Led_Run(void *parmameter) {
  {
    // 初始引脚状态。取决于初始状态的高低电平哪个是灭
    GPIO_ResetPins(LED_RUN_PORT, LED_RUN_PIN);

    led_work_status_sem = rt_sem_create("led_work_status_sem", 0, RT_IPC_FLAG_PRIO);
  }

  for (;;) {
//    if (led_status.led_undefined == 1) {
//      GPIO_ResetPins(LED_RUN_PORT, LED_RUN_PIN);
//      completion_wait(led_work_status_sem, RT_WAITING_FOREVER);
//    }
	  
	  if ( led_status.led_error == 1 ){
		  led_status.led_run = 0;
		  led_status.led_busy = 0;
		  led_status.led_undefined = 0;
	  }
	  
	  if ( led_busy_times != 0 ){
		  led_status.led_busy = 1;
		  led_status.led_run = 0;
		  led_status.led_undefined = 0;
	  }
	  
	  if ( led_status.led_run == 1 ){
		  led_status.led_error = 0;
		  led_status.led_busy = 0;
		  led_status.led_undefined = 0;
	  }


    {// led bsp运行
      if (led_status.led_error == 1) {
        GPIO_SetPins(LED_RUN_PORT, LED_RUN_PIN);
      } else if (led_status.led_busy == 1) {
        GPIO_TogglePins(LED_RUN_PORT, LED_RUN_PIN);
        led_busy_times--;
        if (led_busy_times == 0) {
          led_status.led_busy = 0;
		  led_status.led_run = 1;
		  GPIO_ResetPins(LED_RUN_PORT, LED_RUN_PIN);
        }
		
      } else if ((led_status.led_run == 1) && (led_run_times % 20 == 0)) {
        GPIO_TogglePins(LED_RUN_PORT, LED_RUN_PIN);
      }
	  led_run_times++;
    }// led bsp运行
    rt_thread_mdelay(50);
    SWDT_FeedDog();

    {// 对于该清位的地方清位
    }// 对于该清位的地方清位
  }
}// Led运行函数
